<HTML><HEAD><TITLE>phrase(+Grammar, ?Tokens, ?Remainder)</TITLE>
</HEAD><BODY>[ <A HREF="index.html">Control</A> | <A HREF="../../index.html">Reference Manual</A> | <A HREF="../../fullindex.html">Alphabetic Index</A> ]
<H1>phrase(+Grammar, ?Tokens, ?Remainder)</H1>
Succeeds if Tokens can be parsed as part of the grammar defined in Grammar
and Remainder contains any remaining terms in Tokens.


<DL>
<DT><EM>Grammar</EM></DT>
<DD>Compound Term or Atom.
</DD>
<DT><EM>Tokens</EM></DT>
<DD>List of Prolog terms.
</DD>
<DT><EM>Remainder</EM></DT>
<DD>List of Prolog terms.
</DD>
</DL>
<H2>Description</H2>
   phrase/3 is used to parse grammars (DCGs) defined using the grammar rule
   operator --&gt;.  The flag macro_expansion must be set on when compiling
   grammar rules.

<P>
   Giving a list of terms in Tokens, phrase/3 parses it according to the
   grammar defined in Grammar.  As the terms in Tokens are parsed in order,
   any remaining terms are returned in Remainder.  Further acceptable
   solutions are returned on backtracking.

<H3>Modes and Determinism</H3><UL>
<LI>phrase(+, -, -) is nondet
<LI>phrase(+, +, -) is nondet
<LI>phrase(+, +, +) is semidet
</UL>
<H3>Modules</H3>
This predicate is sensitive to its module context (tool predicate, see @/2).
<H3>Fail Conditions</H3>
The initial terms in Tokens do not belong to Grammar
<H3>Resatisfiable</H3>
Depends on the grammar
<H3>Exceptions</H3>
<DL>
<DT><EM>(4) instantiation fault </EM>
<DD>Grammar is not instantiated.
<DT><EM>(5) type error </EM>
<DD>Grammar is a number or a string.
</DL>
<H2>Examples</H2>
<PRE>
   [eclipse]: [user].
    a --&gt; [].
    a --&gt; [z],a.
    user compiled 212 bytes in 0.03 seconds
   yes.
   [eclipse]: phrase(a,[z,z],[]).

   yes.
   [eclipse]: phrase(a,[z,z,z,y],[z,y]).

   yes.
   [eclipse]: phrase(a,[z,z,y],R).

   R = [z, z, y]     More? (;)

   R = [z, y]     More? (;)

   R = [y]     More? (;)

   no (more) solution.
   [eclipse]: phrase(a,X,[y]).

   X = [y]     More? (;)

   X = [z, y]     More? (;)

   X = [z, z, y]     More? (;)

   X = [z, z, z, y]     More? (;)

   X = [z, z, z, z, y]     More? (;)
   yes.
   [eclipse]:

Fail:
   phrase(a, [z, z, y], []).

Error:
   phrase(X, [what, time, is, it], [is, it],R).  (Error 4).
   phrase("a", X,R).                             (Error 5).
   phrase(456, X,R).                             (Error 5).



</PRE>
<H2>See Also</H2>
<A HREF="../../kernel/control/phrase-2.html">phrase / 2</A>, <A HREF="../../kernel/syntax/current_macro-4.html">current_macro / 4</A>, <A HREF="../../kernel/syntax/macro-3.html">macro / 3</A>, <A HREF="../../kernel/syntax/erase_macro-2.html">erase_macro / 2</A>
</BODY></HTML>
